const fs = require('fs');
const path = require('path');


const http = require('http');
let server = http.createServer()
server.listen(3000, () => {
    console.log("3000监听成功");
})


// 根据用户请求的不同路径，响应不同的内容
server.on("request", (req, res) => {


    if (req.url == "/favicon.ico") {
        res.end()
        return
    }


    let { pathname } = new URL(req.url, "http://localhost:3000/")


    if (pathname == "/index") {


        let filepath = path.join(__dirname, "../html/index.html")
        let result = fs.readFileSync(filepath)
        res.end(result)

    }
    

    // 图片   js   css  统称为静态资源，静态资源可以统一处理
    else if(pathname=="/img/yinghua.webp") {

        let filepath = path.join(__dirname, "../img/yinghua.webp")
        let result = fs.readFileSync(filepath)
        res.end(result)

    }else if(pathname=="/css/index.css") {

        let filepath = path.join(__dirname, "../css/index.css")
        let result = fs.readFileSync(filepath)
        res.end(result)

    }else if(pathname=="/js/index.js") {

        let filepath = path.join(__dirname, "../js/index.js")
        let result = fs.readFileSync(filepath)
        res.end(result)

    }
    
    
    else {


        // 404
        let filepath = path.join(__dirname, "../html/404.html")
        let result = fs.readFileSync(filepath)

        res.writeHead(404, { "Content-type": "text/html;charset=utf-8" })

        res.end(result)

    }

})


// get 
// 地址栏输入，直接按下回车
// href(link  a)
// src(script  img)
// form 表单的get方法

// ajax的get请求

// post
// form 表单的post方法

// ajax的get请求






// get   post   put   delete